package start.redisstream;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.HashMap;
import java.util.Map;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@Slf4j
public class SaticScheduleTask {

    int count = 0;

    @Autowired
    private StringRedisTemplate redisTemplate;


    //@Scheduled(initialDelay = 10000,fixedDelay = 5000)  //延迟10秒执行，方法前后间隔2s
    public void sendRecord() {
        int batchAdd = 10*10000;
        if(count >= 1000*10000) {
            log.info("总共产生[{}]条记录，毁灭吧！", count);
            return;
        }
        log.info("[{}]条记录插入开始", batchAdd);
        for(int i = 0; i< batchAdd; i++) {
            count++;
            /*long length = redisTemplate.opsForStream().info("lzm_stream").streamLength().longValue();
            if(length > 100000) {
                log.info("延迟两秒写入");
                Thread.sleep(2*1000);
            }*/
            Map<String, String> map = new HashMap<>();
            map.put("order", String.valueOf(i+1));
            //map.put("a", "aa1".concat(String.valueOf(i)));
            //map.put("b", "bb1".concat(String.valueOf(i)));
            //map.put("c", "cc1".concat(String.valueOf(i)));
            //map.put("time", String.valueOf(System.currentTimeMillis()));
            RecordId recordId = redisTemplate.opsForStream().add("lzm_stream", map);
            //log.info("添加消息返回结果：{}", recordId);
        }
        log.info("[{}]条记录插入结束，合计插入[{}]", batchAdd, count);
    }
}